import { Env, getEnv } from '@/utils/env';
import * as miniappUtils from '@/utils/miniapp';
import * as orderUtils from '@/utils/order';
import { Modal } from 'antd';
import { PropsWithChildren, useEffect, useMemo } from 'react';
import { useModel } from 'umi';

export function Limit(props: PropsWithChildren<{}>) {
  const env = getEnv();
  const skip = env === Env.Sit || env === Env.Beta;
  const { order } = useModel('bom');
  const isAfterSaleOrder = useMemo(
    () => skip || (order && orderUtils.isAfterSaleOrder(order)),
    [order],
  );
  useEffect(() => {
    if (!order) return;
    if (!isAfterSaleOrder) {
      Modal.warn({
        title: '当前订单无法编辑',
        content:
          '当前订单不是售后单，无法使用物料编辑器。请创建售后单并打开对应的售后单进行编辑',
        okText: '我知道了',
        onOk: () => {
          miniappUtils.close();
        },
      });
    }
  }, [order, isAfterSaleOrder]);
  if (!order || !isAfterSaleOrder) return null;
  return props.children;
}
